# -*- coding: utf-8 -*-

import socket
import thread
import time
import sys
import os
import httplib, urllib
import re

def gGetHtmlLines(url):  
    if url==None : return  
    #if not httpExists(url): return   
    try:  
        page = urllib.urlopen(url)     
        html = page.readlines()  
        page.close()  
        return html  
    except:  
        print "gGetHtmlLines() error!"  
        return ;


def GetUrl(url):
    lines= gGetHtmlLines(url)
    regx = r"""((src)|(href))=(\"|\')?(\S+)(\'|\")"""
    lists = gGetRegList(lines,regx) 
    print lists
    for lnk in lists:
        thread.start_new_thread(LinkThread,(lnk,lnk))

def LinkThread(lnk,lnk2):
    #conn = httplib.HTTPConnection("www.kankanews.com")
    #conn.request("GET", "/")
    #response = conn.getresponse() 
    #print response.status
    #conn.close()
    print lnk
    return 
    


def gGetRegList(linesList,regx):  
    if linesList==None : return   
    #print linesList
    rtnList=[]  
    for line in linesList:  
        matchs = re.search(regx, line, re.IGNORECASE)  
        if matchs!=None:  
            allGroups = matchs.groups()  
            for foundStr in allGroups:  
                if foundStr not in rtnList and foundStr != None and len(foundStr)>4  and foundStr[0]!='#' and foundStr[1]!='#':  
                    foundStr=foundStr.replace("\'","")
                    rtnList.append(foundStr)  
    return rtnList  

if __name__ == '__main__':
    try:
        print u'-'
        print u'LastUpdate: 20120620 R1'
        print u'MgrServ Ver 0.1.0'
        GetUrl("http://www.kankanews.com")
    except KeyboardInterrupt:
        print "\r\nKeyboardInterrupt"
        exit()

